package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class DEmo12 {
    public static void main(String[] args) {
        try (Connection connection = DBUtils.getConn()) {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username = scan.nextLine();
            String sql = "select id from user where username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("用户名已存在!");
                return;
            }
            String sql1 = "insert into user values(null,?,?,?)";
            ps = connection.prepareStatement(sql1);
            ps.setString(1, username);
            System.out.println("请输入密码:");
            ps.setString(2, scan.nextLine());
            System.out.println("请输入昵称:");
            ps.setString(3, scan.nextLine());
            ps.executeUpdate();
            System.out.println("注册成功!");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
